/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.ifrs.poa.control;

import br.edu.ifrs.poa.dao.NoticiaDAO;
import br.edu.ifrs.poa.model.Noticia;
import br.edu.ifrs.poa.model.Usuario;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;

/**
 *
 * @author user
 */
public class CadastroNoticiaProcessor extends Processor {

    @Override
    public void execute() throws ServletException, IOException {
        getResponse().setContentType("text/html;charset=UTF-8");
        try {
            
            PrintWriter out = getResponse().getWriter();

            Noticia news = new Noticia();
            news.setTitulo(getRequest().getParameter("titulo"));
            news.setDescricao(getRequest().getParameter("descricao"));
            news.setLink(getRequest().getParameter("link"));

            // Recupera o objeto de sessão que identifica quem está publicando a noticia
            HttpSession session = getRequest().getSession(false);

            Usuario value = (Usuario) session.getAttribute("usuario");
            news.setUsuario(value);
            NoticiaDAO.addNews(news);
            getResponse().sendRedirect("noticiaCadastrada.jsp");  
        } catch (SQLException ex) {
            throw new ServletException(ex);
        } catch (ClassNotFoundException ex) {
            throw new ServletException(ex);
        }
    }
}
